package com.wangke.util;

import com.alibaba.fastjson.JSONObject;
import com.wangke.model.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

/**
 * @author wangke
 * @version v1.0
 * @项目名称: study
 * @描述:
 * @创建时间: 2019/4/22
 * @update [序号][日期YYYY-MM-DD] [更改人姓名][变更描述]
 */
@Component
@Slf4j
public class KafkaSender {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    //发送消息方法
    public void send() {
        Message message = new Message();
        message.setId(System.currentTimeMillis());
        message.setMsg(UUID.randomUUID().toString());
        message.setSendTime(new Date());
        log.info("+++++++++++++++++++++  message = {}", JSONObject.toJSONString(message));
        kafkaTemplate.send("wkTest",message.getId().toString(), JSONObject.toJSONString(message));
    }
}